﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using OverStore.Runtime.OverStoreContext;
using OverStore.Runtime.ObjectRepository;

namespace OverStore.Configuration.ObjectRepository
{
    /// <summary>
    /// Factory for creating instances of <see cref="RedirectObjectRepository{TInstance, TIdentifier, TRedirectToInstance, TRedirectToIdentifier}"/> type.
    /// </summary>
    /// <typeparam name="TInstance">The type of the instance.</typeparam>
    /// <typeparam name="TIdentifier">The type of the identifier.</typeparam>
    /// <typeparam name="TRedirectToInstance">The type of the redirect to instance.</typeparam>
    /// <typeparam name="TRedirectToIdentifier">The type of the redirect to identifier.</typeparam>
    public class RedirectObjectRepositoryConfiguration<TInstance, TIdentifier, TRedirectToInstance, TRedirectToIdentifier> : IComponentConfiguration<IRepository<TInstance>>,
        IComponentConfiguration<IRepositoryBase>
        where TInstance : class
        where TRedirectToInstance : class, TInstance
        where TRedirectToIdentifier : TIdentifier
    {
        /// <summary>
        /// Creates the new instance of <typeparamref name="TComponent"/> type using set of services provided by <paramref name="context"/> object.
        /// </summary>
        /// <param name="context">The context provides access to services shared between components.</param>
        /// <returns>
        /// The new instance of <typeparamref name="TComponent"/> class.
        /// </returns>
        public IRepository<TInstance> CreateComponent(IOverStoreContext context)
        {
            if (Object.ReferenceEquals(context, null))
                throw new ArgumentNullException("context");
            return new RedirectObjectRepository<TInstance, TIdentifier, TRedirectToInstance, TRedirectToIdentifier>(context.PersistenceServiceProvider);
        }

        /// <summary>
        /// Creates the new instance of <typeparamref name="TComponent"/> type using set of services provided by <paramref name="context"/> object.
        /// </summary>
        /// <param name="context">The context provides access to services shared between components.</param>
        /// <returns>
        /// The new instance of <typeparamref name="TComponent"/> class.
        /// </returns>
        IRepositoryBase IComponentConfiguration<IRepositoryBase>.CreateComponent(IOverStoreContext context)
        {
            return CreateComponent(context);
        }
    }
}
